package com.bolt.report.config;

import com.bolt.common.BoltConstants;
import com.bolt.common.utils.StrUtil;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public interface ReportTokenService {

    default String getToken(HttpServletRequest request) {
        String token = request.getParameter("token");
        if (StrUtil.isBlank(token)) {
            token = request.getHeader(BoltConstants.AUTH_HEADER);
        }
        return token;
    }


    default String getToken() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (null != requestAttributes) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            return getToken(request);
        } else {
            return null;
        }

    }

}